package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxFeedBack;
import com.budwk.app.miniapp.services.WxfeedbackService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/feedback")
@ApiDefinition(tag = "微信小程序-用户反馈")
@Slf4j
public class WxfeedbackController {
    @Inject
    private WxfeedbackService wxFeedbackService;

    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询用户反馈信息")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxFeedBack.class
    )
    @ApiResponses(
            implementation = WxFeedBack.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxFeedBack bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getUserId() != null && Strings.isNotBlank(bean.getUserId().toString())) {
            cnd.and("userId", "=", bean.getUserId());
        }

        if (bean.getContent() != null && Strings.isNotBlank(bean.getContent().toString())) {
            cnd.and("content", "=", bean.getContent());
        }

        if (bean.getSrc() != null && Strings.isNotBlank(bean.getSrc().toString())) {
            cnd.and("src", "=", bean.getSrc());
        }

        return Result.data(wxFeedbackService.listPage(pageNo, pageSize, cnd));
    }

    @At("/add")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增用户反馈信息")
    @ApiFormParams(
            implementation = WxFeedBack.class
    )
    @ApiResponses
    @SLog(tag = "新增用户反馈信息", value = "${args[0].feedId}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxFeedBack wxFeedback, HttpServletRequest req) {
        wxFeedback.setCreatedBy(SecurityUtil.getUserId());
        wxFeedbackService.insert(wxFeedback);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改用户反馈信息")
    @ApiFormParams(
            implementation = WxFeedBack.class
    )
    @ApiResponses
    @SLog(tag = "修改用户反馈信息", value = "${args[0].feedId}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxFeedBack wxFeedback, HttpServletRequest req) {
        wxFeedback.setUpdatedBy(SecurityUtil.getUserId());
        wxFeedbackService.updateIgnoreNull(wxFeedback);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取用户反馈信息")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    //@SaCheckPermission("miniapp.feedback")
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxFeedBack wxFeedback = wxFeedbackService.fetch(id);
        if (wxFeedback == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxFeedback);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除用户反馈信息")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除用户反馈信息", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxFeedBack wxFeedback = wxFeedbackService.fetch(id);
        if (wxFeedback == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxFeedbackService.delete(id);
        req.setAttribute("_slog_msg", wxFeedback.getFeedId());
        return Result.success();
    }
}
